import { apiInit } from '@/services'
import { IFetchNoticesList, IFetchRegulationList } from '../../type'
import { ref, onMounted, unref, computed } from 'vue'
import { useRouter } from 'vue-router'
import { useLoading } from '@/hooks/useLoading'
export default function useDetails() {
	const api = apiInit()
	const detailList = ref([])
	const defaultLoading = ref({ loading: false })
	const tenantDetail = computed(() =>
		JSON.parse(localStorage.getItem('tenantDetail') || '')
	)

	const { getLoading, setLoading } = useLoading(defaultLoading)
	const fetchNoticeList = async ({
		page = {},
		query = {},
	}: IFetchNoticesList) => {
		setLoading(true)
		const { data, code } = await api.default.ListNoticePrivateV2POST({
			payload: { page, query },
		})
		setLoading(false)
		if (code === '000000') {
			const list: any = data
			list.rows.map((item: any, index: number) => {
				item.id = item.notice_id
				return item
			})
			return list.rows
		}
	}

	const fetchRegulationList = async ({
		page = {},
		query = {},
	}: IFetchRegulationList) => {
		setLoading(true)
		const { data, code } = await api.default.ListRegulationPrivateV2POST({
			payload: { page, query },
		})
		setLoading(false)
		if (code === '000000') {
			const list: any = data
			list.rows.map((item: any, index: number) => {
				item.id = item.rules_id
				return item
			})
			return list.rows
		}
	}
	const page = {
		page_index: 1,
		page_size: 5,
	}
	const query = {
		category_id: '',
		create_time: '',
		creator: '',
		end_time: '',
		title: '',
	}
	const active = ref('notifications')
	let informationValue = computed(
		() => tenantDetail.value.data.information_module
	)
	let processValue = computed(() => tenantDetail.value.data.process_quick_entry)
	const tabClick = async (item: any) => {
		active.value = item.value
		if (active.value === 'notifications') {
			detailList.value = await fetchNoticeList({ page, query })
			detailList.value =
				detailList.value?.length >= 5 && processValue.value !== 1
					? detailList.value.slice(0, 5)
					: informationValue.value && processValue.value
					? detailList.value.slice(0, 3)
					: detailList.value
		} else {
			detailList.value = await fetchRegulationList({
				page,
				query: {
					rule_id: '',
					category_id: '',
					create_time: '',
					creator: '',
					end_time: '',
					title: '',
				},
			})
			detailList.value =
				detailList.value?.length >= 5 && processValue.value !== 1
					? detailList.value.slice(0, 5)
					: informationValue.value && processValue.value
					? detailList.value.slice(0, 3)
					: detailList.value
		}
	}
	const router = useRouter()
	const toMore = () => {
		router.push({
			name: 'AttachmentCenter',
			query: { type: active.value },
		})
	}
	//通知公告点击数统计
	const notificationsCountHandle = (notice_id: any) => {
		api.default.CountHitsNoticePrivateV2POST({
			payload: { notice_id },
		})
	}
	//规章制度点击数统计
	const regulationsCountHandle = (rules_id: any) => {
		api.default.CountHitsRegulationPrivateV2POST({
			payload: { rules_id },
		})
	}
	const handeleDetail = (params: any) => {
		router.push({
			name: 'AttachmentDetail',
			query: { type: active.value, id: params.id },
		})
		unref(active) === 'notifications'
			? notificationsCountHandle(params.id)
			: regulationsCountHandle(params.id)
	}

	onMounted(async () => {
		getMenuTitle()
		detailList.value = await fetchNoticeList({ page, query })
		detailList.value =
			detailList.value?.length >= 5 && processValue.value !== 1
				? detailList.value.slice(0, 5)
				: informationValue.value && processValue.value
				? detailList.value.slice(0, 3)
				: detailList.value
	})

	const noticeTitle = ref({})
	const ruleTitle = ref({})
	const tabList: any = ref([])
	// 获取模块标题
	const getMenuTitle = async () => {
		const { data } = await api.portalExtend.POSTInfoModuleList({})
		for (let key in data) {
			if (data[key]?.module_id === 'NOTICE') {
				noticeTitle.value = data[key].name
			}
			if (data[key]?.module_id === 'RULES') {
				ruleTitle.value = data[key].name
			}
		}
		sessionStorage.setItem('infoModuleList', JSON.stringify(data))
		const local: any = localStorage.getItem('locale')?.slice(0, 2)
		tabList.value = [
			{
				label: noticeTitle.value ? noticeTitle.value[local] : '',
				value: 'notifications',
			},
			{
				label: ruleTitle.value ? ruleTitle.value[local] : '',
				value: 'regulations',
			},
		]
	}

	return {
		detailList,
		active,
		tabClick,
		toMore,
		handeleDetail,
		getLoading,
		tabList,
		tenantDetail,
	}
}
